﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Model;
using System.IO;
public partial class SchoolManagement_UpdateSchoolInfo : AdminUserBase
{
    public int currentSchoolId = 0;
    public V_School currentSchool;
    public string LogImg;
    public string SchoolFile;
    protected void Page_Load(object sender, EventArgs e)
    {
        switch (this.AdminType)
        {
            case 0:
                this.txtVS_name.ReadOnly = false;
                break;
            case 1:
            case 2:
            case 3:
            case 4:
                this.txtVS_name.ReadOnly = true;
                break;
        }
        ShowUpdateSchoolInfo();
        if (!IsPostBack)
        {
            if (SchoolID > 0)
            {
               
            }

            BindSchoolInfo();

        }

    }

    private void BindSchoolInfo()
    {
        txtVS_name.Text = currentSchool.VS_name;
        ddlSchoolState.SelectedValue = currentSchool.VS_state.ToString();
        txtVS_phone.Text = currentSchool.VS_phone;
        txtVS_englishname.Text = currentSchool.VS_englishname;
        txtVS_zipcode.Text = currentSchool.VS_zipcode;
        txtVS_city.Text = currentSchool.VS_city;
        txtdesc.Text = currentSchool.Vs_desc;
        txtVS_province.Text = currentSchool.VS_province;
        txtVS_relanameemail.Text = currentSchool.VS_relanameemail;
        txtVS_relanamephone.Text = currentSchool.VS_relanamephone;
        txtVS_relanname.Text = currentSchool.VS_relanname;
        txtVS_zipcode.Text = currentSchool.VS_zipcode;
        txtVS_phone.Text = currentSchool.VS_phone;
        txtVS_address.Text = currentSchool.VS_address;
        txtWeb.Text = currentSchool.Vs_web;

        string dictionarySavedPath = PageHelper.GetWebConfigValue("SchollImageHttp") + currentSchool.VS_logofile;

        string schoolImgPath = PageHelper.GetWebConfigValue("SchollImageHttp") + currentSchool.Vs_SchoolImg;
        //PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString()) + currentSchool.VS_logofile;


        if (!string.IsNullOrEmpty(currentSchool.VS_logofile) || !string.IsNullOrEmpty(currentSchool.Vs_SchoolImg))
        {
            string js = string.Empty;
            if (!string.IsNullOrEmpty(currentSchool.VS_logofile))
            {
                LogImg = dictionarySavedPath;
            }
            if (!string.IsNullOrEmpty(currentSchool.Vs_SchoolImg))
            {
                //js += "PreviewImage('" + schoolImgPath + "');";
                SchoolFile = schoolImgPath;
            }
            if (!string.IsNullOrEmpty(js))
            {
                LogImg = "";
            }
            if (!string.IsNullOrEmpty(js))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", js, true);
            }
        }
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(FileUpload1.FileName))//修改图片
        {
            string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
            string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
            dictionarySavedPath += "\\Images\\SchoolId\\";
            dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", currentSchoolId.ToString());
            if (!Directory.Exists(dictionarySavedPath))
            {
                Directory.CreateDirectory(dictionarySavedPath);
            }
            // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
            //保存logo的文件夹绝对路径

            string logfileSavedPath = dictionarySavedPath + "logo" + fileExtension;
            //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
            FileUpload1.SaveAs(logfileSavedPath);

            currentSchool.VS_logofile = ("/Images/SchoolId/logo" + fileExtension).Replace("SchoolId", currentSchoolId.ToString());
          
        }
        if (!string.IsNullOrEmpty(this.fuSchoolIntroduction.FileName))//修改图片
        {
            string fileExtension = System.IO.Path.GetExtension(fuSchoolIntroduction.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
            string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
            dictionarySavedPath += "\\Images\\SchoolId\\";
            dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", currentSchoolId.ToString());
            if (!Directory.Exists(dictionarySavedPath))
            {
                Directory.CreateDirectory(dictionarySavedPath);
            }
            // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
            //保存logo的文件夹绝对路径

            string logfileSavedPath = dictionarySavedPath + "SchoolImg" + fileExtension;
            //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
            this.fuSchoolIntroduction.SaveAs(logfileSavedPath);

            currentSchool.Vs_SchoolImg = ("/Images/SchoolId/SchoolImg" + fileExtension).Replace("SchoolId", currentSchoolId.ToString());

        }

        currentSchool.update_time = DateTime.Now;
        currentSchool.VS_address = txtVS_address.Text;
        currentSchool.VS_city = txtVS_city.Text;
        currentSchool.VS_englishname = txtVS_englishname.Text;
        currentSchool.VS_name = txtVS_name.Text;
        currentSchool.VS_phone = txtVS_phone.Text;
        currentSchool.VS_province = txtVS_province.Text;
        currentSchool.VS_relanameemail = txtVS_relanameemail.Text;
        currentSchool.VS_relanamephone = txtVS_relanamephone.Text;
        currentSchool.VS_relanname = txtVS_relanname.Text;
        currentSchool.Vs_desc = txtdesc.Text;
        currentSchool.VS_state = Convert.ToInt32(ddlSchoolState.SelectedValue.Trim());
        currentSchool.Vs_web = txtWeb.Text; 
        currentSchool.VS_zipcode = txtVS_zipcode.Text;
        SchoolModule sm = new SchoolModule();
        int result = sm.UpdateSchool(currentSchool);

        if (result == 1)
        {

            PageHelper.AlterMsgAndRedirect("修改成功！", "SearchAndSchool.aspx");
        }
        else
        { PageHelper.AlterMsg("修改失败！"); }

    }
    private void ShowUpdateSchoolInfo()
    {

        SchoolModule sm = new SchoolModule();
        if (Request.QueryString["SID"] != null)
        {
            currentSchoolId = int.Parse(Request.QueryString["SID"].ToString());
        }
        else
        {
            currentSchoolId = this.SchoolID;
        }
        currentSchool = sm.GetSchoolModelById(currentSchoolId);

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        this.txtdesc.Text = string.Empty;
        this.txtVS_address.Text = string.Empty;
        this.txtVS_city.Text = string.Empty;
        this.txtVS_englishname.Text = string.Empty;
        this.txtVS_name.Text = string.Empty;
        this.txtVS_phone.Text = string.Empty;
        this.txtVS_province.Text = string.Empty;
        this.txtVS_relanameemail.Text = string.Empty;
        this.txtVS_relanamephone.Text = string.Empty;
        this.txtVS_relanname.Text = string.Empty;
        this.txtVS_zipcode.Text = string.Empty;
        this.ddlSchoolState.SelectedIndex = 0; 
    }
}
